//
//  CSearchConditionView.m
//  Delicious
//
//  Created by yilong zhang on 11-11-30.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CSearchConditionView.h"

@implementation CSearchCell

@synthesize delegate;
@synthesize m_Btn;

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier Size:(CGSize)size
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        
        m_Btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        m_Btn.frame = CGRectMake(5, 0, size.width-10, size.height);
        [m_Btn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        m_Btn.backgroundColor = [UIColor clearColor];
        [self addSubview:m_Btn];
        
        delegate = nil;
    }
    
    return self;
}

-(void)OnButtonDown:(UIButton*)sender
{
    [delegate DidSearchCellSelected:sender.tag Text:sender.titleLabel.text];
}

@end

@implementation CSearchConditionView

@synthesize m_fHeight;
@synthesize m_Items;
@synthesize m_Table;
@synthesize m_Delegate;
@synthesize m_nIndex;

- (id)initWithFrame:(CGRect)frame Items:(NSArray*)items
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        m_Items = [[NSMutableArray alloc] initWithArray:items];
        
        m_fHeight = [items count]*frame.size.height;
        m_fHeight = (m_fHeight > 155) ? (155) : (m_fHeight);
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, m_fHeight);
        
        m_Table = [[UITableView alloc] initWithFrame:CGRectMake(0, 5, self.frame.size.width, m_fHeight)];
        m_Table.scrollEnabled = YES;
        m_Table.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        m_Table.autoresizesSubviews = YES;
        m_Table.delegate   = self;
        m_Table.dataSource = self;
        m_Table.separatorColor = [UIColor clearColor];
        [self addSubview:m_Table];
        [m_Table release];
        
        self.backgroundColor = [UIColor whiteColor];
        self.m_nIndex = 0;
    }
    return self;
}

-(void)dealloc
{
    [m_Items release];
    
    [super dealloc];
}

-(void)ShowSearchConditionView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, m_fHeight);
    [UIView commitAnimations];
}

-(void)HiddenSearchConditionView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 0);
    [UIView commitAnimations];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_Items count];
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCellStyle style = UITableViewCellStyleValue1;
	
	CSearchCell *cell = (CSearchCell*)[tableView dequeueReusableCellWithIdentifier:@"BaseCell"];
	
	if (nil == cell)
	{
		cell = [[CSearchCell alloc] initWithStyle:style reuseIdentifier:@"BaseCell" Size:CGSizeMake(self.frame.size.width, 28)];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
    }
    
    [cell.m_Btn setTitle:[m_Items objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    cell.m_Btn.tag = indexPath.row;
    
    return cell;
}

-(void)DidSearchCellSelected:(int)index Text:(NSString *)text
{
    m_nIndex = index;
    [m_Delegate DidSearchConditionViewSelected:index Text:text];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30.0;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
